package com.example.grape_effect.effet.sticker

import android.opengl.GLES20
import com.example.grape_effect.gl.common.GLProgram
import com.example.grape_effect.gl.common.PkmTexture
import com.example.grape_effect.gl.common.ShaderConstant
import com.example.grape_effect.gl.common.ShaderParameter


//zip可以单独弄成一个纹理，纹理上传的时候，先ready，然后绘制，把读zip的逻辑移到 texutre中即可
class StickerEffect: GLProgram() {
    lateinit var stickPath:String

    lateinit var texture: PkmTexture
    init {
        name = "stickerEffect"
        stickPath = "assets/cc.zip"
        texture = PkmTexture(stickPath)
    }
    override var vertexShader = ShaderConstant.FLIP_Y_FILTER_VERTEX_SHADER
    override var fragmentShader = """
        precision mediump float;
        varying vec2 textureCoordinate;
        uniform sampler2D inputTexture;
        uniform sampler2D vTextureAlpha;

        void main() {
            vec4 color=texture2D( inputTexture, textureCoordinate);
            color.a=texture2D(vTextureAlpha,textureCoordinate).r;
            gl_FragColor = color;
        }
    """.trimIndent()
    override fun addParameters() {
        parameters["vPosition"] = ShaderParameter(
            "vPosition",
            ShaderParameter.TYPE_ATTRIBUTE,
            ShaderParameter.VALUE_TYPE_ATTRIBUTE_PARAMETER
        )
        parameters["inputTextureCoordinate"] = ShaderParameter(
            "inputTextureCoordinate",
            ShaderParameter.TYPE_ATTRIBUTE,
            ShaderParameter.VALUE_TYPE_ATTRIBUTE_PARAMETER
        )
        parameters["inputTexture"] = ShaderParameter(
            "inputTexture",
            ShaderParameter.TYPE_TEXTURE,
            ShaderParameter.VALUE_TYPE_IMAGE_TEXTURE
        )
        parameters["vTextureAlpha"] = ShaderParameter(
            "vTextureAlpha",
            ShaderParameter.TYPE_TEXTURE,
            ShaderParameter.VALUE_TYPE_IMAGE_TEXTURE
        )
    }

    override fun setDefaultParameterValues() {
        parameters["vPosition"]?.value = ShaderParameter.AttributeParameter(ShaderConstant.POSITIONS, 2)
        parameters["inputTextureCoordinate"]?.value = ShaderParameter.AttributeParameter(
            ShaderConstant.COORDINATE, 2)
        // 置null，就不会自动set进shader，交给prepareToDraw去做
        parameters["inputTexture"]!!.value = null
        parameters["vTextureAlpha"]!!.value = null

    }
    private fun updateTexture(){
        GLES20.glEnable(GLES20.GL_BLEND)
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA)
        texture.prepareToDraw( parameters["inputTexture"]!!.location,  parameters["vTextureAlpha"]!!.location)
    }


    override fun draw() {
        clearColor()
        initProgram()
        GLES20.glUseProgram(program)
        updateTexture()
        setParametersToShader()
        GLES20.glEnableVertexAttribArray(parameters["vPosition"]!!.location)
        GLES20.glEnableVertexAttribArray(parameters["inputTextureCoordinate"]!!.location)
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
        GLES20.glDisableVertexAttribArray(parameters["vPosition"]!!.location)
        GLES20.glDisableVertexAttribArray(parameters["inputTextureCoordinate"]!!.location)
    }



}